package com.identify.identifydrawings.netUtils;




import android.util.Log;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.GetBuilder;
import com.zhy.http.okhttp.builder.PostFormBuilder;
import com.zhy.http.okhttp.builder.PostStringBuilder;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class HttpHelper {

    /**
     * 执行不带参数的get请求
     */
//    public static void get(String url, HashMap<Object, Object> map, RequestListener callBack) {
//        get(url, null, callBack);
//    }

    /**
     * 执行不带参数的post请求
     */
    public static void post(String url, RequestListener callBack) {
        post(url, null, callBack);
    }

    /**
     * 执行带参数的get请求
     */
    public static void get(final String url, Map<Object, Object> map,
                           final RequestListener callBack) {
//        for (Map.Entry<Object, Object> entry : map.entrySet()) {
//            LogUtils.e("TAG"+"key-="+entry.getKey().toString()+";value="+ entry.getValue().toString());
//        }
        getBuilder(url, map).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callBack.onError(call, e,id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callBack.onResponse(response);
                    }





                });
    }
    /**
     * 执行带参数的get请求
     */
    public static void getByte(final String url, Map<Object, Object> map,
                           final RequestListener callBack) {
//        for (Map.Entry<Object, Object> entry : map.entrySet()) {
//            LogUtils.e("TAG"+"key-="+entry.getKey().toString()+";value="+ entry.getValue().toString());
//        }
        getBuilder(url, map).build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        InputStream inputStream = response.body().byteStream();
                        Log.e("TAG","parseNetworkResponse="+inputStream);
                        if(inputStream!=null){
                            callBack.onInputStream(inputStream);
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callBack.onError(call,e,id);
                    }

                    @Override
                    public void onResponse(Object response, int id) {
                            callBack.onResponse(response);
                    }
    });
    }
    /**
     * 执行带参数的get请求
     */
    public static void get(final String url, Map<Object, Object> map, Map<Object, Object> header,
                           final RequestListener callBack) {
        getBuilder(url, map, header).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callBack.onError(call, e,id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callBack.onResponse(response);
                    }

                });
    }

    /**
     * 执行需要缓存数据的post请求
     */
    public static void postCache(final String url, Map<Object, Object> map,
                                 final RequestEnd callBack) {
        postBuilder(url, map).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callBack.onError(call, e,id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callBack.onResponse(response);
                    }


                });
        callBack.onRequest(url);
    }

    /**
     * 执行带参数的post请求
     */
    public static void post(final String url, Map<Object, Object> map,
                            final RequestListener callBack) {
        postBuilder(url, map)
                .build()
//                .readTimeOut(READ_TIMEOUT)//设置读取超时时间
//                .writeTimeOut(WRITE_TIMEOUT)
//                .connTimeOut(CONNECT_TIMEOUT)//设置连接超时时间
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callBack.onError(call,e,id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callBack.onResponse(response);
                    }

                });
    }


    /**
     * 执行带参数的post请求
     */
    public static void post(final String url, Map<Object, Object> map, Map<Object, Object> header,
                            final RequestListener callBack) {
        postBuilder(url, map, header).build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        callBack.onError(call,e,id);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        callBack.onResponse(response);
                    }


                });
    }







    /**
     * 带参数的postStringBuilder
     */
    private static PostStringBuilder postStringBuilder(String url,
                                                       Map<Object, Object> map) {
        PostStringBuilder builder = postBuilderString(url);
        if (null != map && map.size() > 0) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                builder = builder.content(JsonUtils.parseMapToJson(map));
            }
        }
        return builder;
    }

    /**
     * 带参数的postBuilder
     */
    private static PostFormBuilder postBuilder(String url,
                                               Map<Object, Object> map) {
        PostFormBuilder builder = postBuilder(url);
        if (null != map && map.size() > 0) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                builder = builder.addParams(entry.getKey().toString(), entry.getValue().toString());
            }
        }
        return builder;
    }

    /**
     * 带参数的postBuilder
     */
    private static PostFormBuilder postBuilder(String url,
                                               Map<Object, Object> map, Map<Object, Object> header) {
        PostFormBuilder builder = postBuilder(url);
        if (null != map && map.size() > 0) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                builder = builder.addParams(entry.getKey().toString(), entry.getValue().toString());
            }
        }
        if (null != header && header.size() > 0) {
            for (Map.Entry<Object, Object> entry : header.entrySet()) {
                builder = builder.addHeader(entry.getKey().toString(), entry.getValue().toString());
            }
        }
        return builder;
    }

    /**
     * 带参数的getBuilder
     */
    private static GetBuilder getBuilder(String url, Map<Object, Object> map) {
        GetBuilder builder = getBuilder(url);
        if (null != map && map.size() > 0) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                builder = builder.addParams(entry.getKey().toString(), entry.getValue().toString());
            }
        }
        return builder;
    }

    /**
     * 带参数的getBuilder
     */
    private static GetBuilder getBuilder(String url, Map<Object, Object> map, Map<Object, Object> header) {
        GetBuilder builder = getBuilder(url);
        if (null != map && map.size() > 0) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                builder = builder.addParams(entry.getKey().toString(), entry.getValue().toString());
            }
        }
        if (null != header && header.size() > 0) {
            for (Map.Entry<Object, Object> entry : header.entrySet()) {
                builder = builder.addHeader(entry.getKey().toString(), entry.getValue().toString());
            }
        }
        return builder;
    }

    /**
     * 不带参数的postBuilder
     */
    private static PostFormBuilder postBuilder(String url) {
        return OkHttpUtils.post().url(url);
    }

    /**
     * 不带参数的getBuilder
     */
    private static GetBuilder getBuilder(String url) {
        return OkHttpUtils.get().url(url);
    }

    /**
     * 不带参数的postStringBuilder
     */
    private static PostStringBuilder postBuilderString(String url) {
        return OkHttpUtils.postString().url(url);
    }


    public void download(){

    }



}
